package sz.com.postoffice.jxyzwgcase.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sun.istack.NotNull;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import sz.com.postoffice.jxyzwgcase.core.TableDataInfo;
import sz.com.postoffice.jxyzwgcase.pojo.*;
import sz.com.postoffice.jxyzwgcase.service.DailyIncomeSummaryService;
import sz.com.postoffice.jxyzwgcase.service.EmployeeService;
import sz.com.postoffice.jxyzwgcase.utils.DateStrUtil;
import sz.com.postoffice.jxyzwgcase.utils.StringUtils;

import java.util.List;

/**
 * @ClassName PersonnelSectionController
 * @Deacription TODO
 * @Author 王盟
 * @Date 2020/9/28 13:06
 * @Version 1.0
 **/
@Api(value = "人员信息维护API", tags = "{人员信息维护API}")
@RestController
@Slf4j
@RequestMapping(value = "/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private DailyIncomeSummaryService dailyIncomeSummaryService;

    /*
    ** 
     * @description: 人员及段道信息（营业部填报） 新增
     * @param:  * @param information 
     * @return  boolean
     * @author liuyi
     * @date: 2020/9/28 13:46
     */
    @ApiOperation(value = "新增段道与营业部员工信息", notes = "新增段道与营业部员工信息")
    @RequestMapping(value = "/employees", method = RequestMethod.POST)
    public TableDataInfo insert(@RequestBody Employee information) {
        System.out.println("开始新增...");
        Employee employee = employeeService.findByEntity(information);
        TableDataInfo tableDataInfo = new TableDataInfo();
        if (employee != null ) {
            tableDataInfo.setCode(400);
            tableDataInfo.setMsg("该账号已存在");
            return tableDataInfo;
        }
        employeeService.insert(information);
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("新增成功");
        return tableDataInfo;
    }
    /*
    ** 
     * @description: 人员及段道信息（营业部填报） update
     * @param:  * @param information
     * @return  boolean
     * @author liuyi
     * @date: 2020/9/28 13:46
     */
    @ApiOperation(value = "更新段道与营业部员工信息", notes = "更新段道与营业部员工信息")
    @RequestMapping(value = "/employees", method = RequestMethod.PUT)
    public boolean update(@RequestBody Employee information) {
        System.out.println("开始更新...");
        return employeeService.update(information);
    }

    /*
    **
     * @description: 人员及段道信息（营业部填报） delete
     * @param:  * @param information
     * @return  boolean
     * @author liuyi
     * @date: 2020/9/28 13:46
     */
    @ApiOperation(value = "删除段道与营业部员工信息", notes = "删除段道与营业部员工信息")
    @RequestMapping(value = "/employees/{pid}", method = RequestMethod.DELETE)
    public boolean delete(@PathVariable("pid") @NotNull @ApiParam(name="人员及段道id",value="path传入",required=true) Integer pid)  {
        System.out.println("开始删除...");
        return employeeService.deleteByPrimaryKey(pid);
    }

   /*
   **
    * @description: 人员及段道信息（营业部填报）查询
    * @param:  * @param information
    * @return  sz.com.postoffice.jxpostoffice.core.TableDataInfo
    * @author liuyi
    * @date: 2020/9/28 13:48
    */
    @ApiOperation(value = "查询段道与营业部员工信息", notes = "查询段道与营业部员工信息")
    @RequestMapping(value = "/employees", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", dataType = "int", paramType = "path", required = true),
            @ApiImplicitParam(name = "pageNum", value = "当前页", dataType = "int", paramType = "path", required = true)
    })
    public TableDataInfo findByInformation(Employee information) {
        System.out.println("开始查询...");
        return employeeService.findByListEntity(information);
    }
    
    @ApiOperation(value = "查找单个段道与营业部员工信息", notes = "查找单个段道与营业部员工信息")
    @RequestMapping(value = "/findperByid/{pid}", method = RequestMethod.GET)
    public Employee findperByid(@PathVariable("pid") @NotNull @ApiParam(name="人员及段道id",value="path传入",required=true) Integer pid)  {
        System.out.println("开始删除...");
        return employeeService.findByPrimaryKey(pid);
    }

    @ApiOperation(value = "新增员工休假信息", notes = "新增员工休假信息")
    @RequestMapping(value = "/employees/vacation", method = RequestMethod.POST)
    public boolean insertVacation(@RequestBody Vacation vacation) {
        System.out.println("开始新增...");
        DailyIncomeSummary param = new DailyIncomeSummary();
        param.setUserCode(vacation.getEmpCode());
        param.setStatDate(vacation.getVacationDate());
        boolean isNewDailySummary = true;
        DailyIncomeSummary dailyIncomeSummary = dailyIncomeSummaryService.findByEntity(param);
        if (dailyIncomeSummary == null) {
            dailyIncomeSummary = new DailyIncomeSummary();
            DailyIncome dailyIncome = setVacationSummary(vacation, dailyIncomeSummary);
            isNewDailySummary = dailyIncomeSummaryService.insertSummaryByDaily(dailyIncome);
        } else {
            DailyIncome dailyIncome = setVacationSummary(vacation, dailyIncomeSummary);
            isNewDailySummary = dailyIncomeSummaryService.updateTodayIncome(dailyIncome);
        }
        return isNewDailySummary && employeeService.insertVacation(vacation);
    }

    @ApiOperation(value = "查询员工休假信息", notes = "查询员工休假信息")
    @RequestMapping(value = "/employees/vacation", method = RequestMethod.GET)
    public List<Vacation> getVacation(Vacation vacation) {
        System.out.println("开始查询...");
        if (StringUtils.isEmpty(vacation.getStartDate()) || StringUtils.isEmpty(vacation.getEndDate())) {
            return null;
        }
        return employeeService.findVacationByPeriod(vacation);
    }

    @ApiOperation(value = "删除员工休假信息", notes = "删除员工休假信息")
    @RequestMapping(value = "/employees/vacation", method = RequestMethod.DELETE)
    public boolean deleteVacation(Vacation vacation) {
        System.out.println("开始修改...");
        DailyIncomeSummary paramSummary = new DailyIncomeSummary();
        paramSummary.setUserCode(vacation.getEmpCode());
        paramSummary.setStatDate(vacation.getVacationDate());
        DailyIncomeSummary dailyIncomeSummary = dailyIncomeSummaryService.findByEntity(paramSummary);
        boolean isSummary = true;
        if (dailyIncomeSummary != null) {
            isSummary = deleteVacationSummary(dailyIncomeSummary, vacation);
        }
        return isSummary && employeeService.deleteVacationById(vacation.getId());
    }

    private DailyIncome setVacationSummary(Vacation vacation,DailyIncomeSummary dailyIncomeSummary) {
        dailyIncomeSummary.setDeptCode(vacation.getEmpDeptCode());
        dailyIncomeSummary.setUserCode(vacation.getEmpCode());
        dailyIncomeSummary.setUserName(vacation.getEmpName() + "(休假)");
        dailyIncomeSummary.setDailyDelivery(0);
        dailyIncomeSummary.setDailyVisit(0);
        dailyIncomeSummary.setExpressCount(0);
        dailyIncomeSummary.setExpressIncome(0.0);
        dailyIncomeSummary.setFragmentedMailCount(0);
        dailyIncomeSummary.setFragmentedMailIncome(0.0);
        dailyIncomeSummary.setStandardMailCount(0);
        dailyIncomeSummary.setStandardMailIncome(0.0);
        dailyIncomeSummary.setIsVacation(1);
        dailyIncomeSummary.setStatDate(vacation.getVacationDate());
        DailyIncome dailyIncome = new DailyIncome(dailyIncomeSummary);
        return dailyIncome;
    }

    private boolean deleteVacationSummary(DailyIncomeSummary dailyIncomeSummary, Vacation vacation) {
        dailyIncomeSummary.setUserName(vacation.getEmpName());
        dailyIncomeSummary.setIsVacation(0);
        return dailyIncomeSummaryService.update(dailyIncomeSummary);
    }
}
